package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.bean.User;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.clinet.cache.Log;

public class SartAct extends BaseAct {
	private Context mContext = SartAct.this;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainact);
		 initPush();
		initDeviceInfo();
		getUser();
		// initData();
		// initData();
		if (mUser.isRemember() && mUser.isAutologin()) {
			Message mMessage = refreshHander.obtainMessage(0);
			refreshHander.removeMessages(0);
			refreshHander.sendMessageDelayed(mMessage, 3000);
		} else {
			Message mMessage = refreshHander.obtainMessage(1);
			refreshHander.removeMessages(1);
			refreshHander.sendMessageDelayed(mMessage, 3000);
		}

	}

	public static String deviceName = "";
	public static String deviceType = "";
	public static String deviceNum = "";

	TelephonyManager telephonyManager = null;

	private void initDeviceInfo() {
		deviceName = Build.MODEL;
		if (telephonyManager == null) {
			telephonyManager = (TelephonyManager) this
					.getSystemService(Context.TELEPHONY_SERVICE);
		}
		deviceType = Build.MODEL;
		devId = telephonyManager.getDeviceId();
		deviceNum = devId;
	}

	public static String devId;

	private void initPush() {
		// 获取设备Id
		devId = "null";
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		if (telephonyManager != null) {
			devId = telephonyManager.getDeviceId();
		}

		startAPNS();

	}

	private void startAPNS() {
		Log.i("----startAPNS-----" + devId);
//		// 启动服务
//		Intent intent = new Intent(APNService.START);
//		intent.putExtra("ch", "CH1152743"); // channel name, 登录开发者页面查看
//		intent.putExtra("devId", devId); // 请确保 devId 不为空, devId 可为任何标识此用户的
//		startService(intent);
	}

	protected void onDestroy() {
		super.onDestroy();
//		stopAPNS();
	};

	private DataService mDataService = null;
	private User mUser = null;

	private void getUser() {
		Log.i("-----------getUser--------");
		mDataService = new DataService(mContext);
		if (mDataService.hasInitUser()) {
			Log.i("-----------hasInitUser--------");
			mUser = mDataService.getCurrUser();
			Log.i("-----------mUser--------" + mUser);
		} else {
			Log.i("---new--------saveUser--------");
			mUser = new User();
			mUser.deviceName = deviceName;
			mUser.deviceNum = devId;
			mUser.deviceType = deviceType;
			mUser.current = "true";
			Log.i("-----------mUser--------" + mUser);
			mDataService.saveUser(mUser);
		}
	}

	private void initData() {
		BirdMessage mBirdMessage1 = new BirdMessage();
		mBirdMessage1.birdName = "白枕鹤";
		mBirdMessage1.pinyin = "bzh";
		BirdMessage mBirdMessage2 = new BirdMessage();
		mBirdMessage2.birdName = "绿头鸭";
		mBirdMessage2.pinyin = "lty";
		BirdMessage mBirdMessage3 = new BirdMessage();
		mBirdMessage3.birdName = "牛背鹭";
		mBirdMessage3.pinyin = "nbl";
		BirdMessage mBirdMessage4 = new BirdMessage();
		mBirdMessage4.birdName = "斑嘴鸭";
		mBirdMessage4.pinyin = "bzy";
		BirdMessage mBirdMessage5 = new BirdMessage();
		mBirdMessage5.birdName = "绿翅鸭";
		mBirdMessage5.pinyin = "lcy";
		// BirdMessage mBirdMessage11=new BirdMessage();
		// mBirdMessage1.birdName="白枕鹤b";
		//
		// BirdMessage mBirdMessage21=new BirdMessage();
		// mBirdMessage2.birdName="绿头鸭l";
		//
		// BirdMessage mBirdMessage31=new BirdMessage();
		// mBirdMessage3.birdName="牛背鹭n";
		//
		// BirdMessage mBirdMessage41=new BirdMessage();
		// mBirdMessage4.birdName="斑嘴鸭B";
		//
		// BirdMessage mBirdMessage51=new BirdMessage();
		// mBirdMessage5.birdName="绿翅鸭L";
		BirdList mBirdList = new BirdList();
		mBirdList.addBirdMessage(mBirdMessage1);
		mBirdList.addBirdMessage(mBirdMessage2);
		mBirdList.addBirdMessage(mBirdMessage3);
		mBirdList.addBirdMessage(mBirdMessage4);
		mBirdList.addBirdMessage(mBirdMessage5);
		mDataService.saveBirdMessage(mBirdList);
		// mDataService.saveBirdMessage(mBirdMessage1);
		// mDataService.saveBirdMessage(mBirdMessage2);
		// mDataService.saveBirdMessage(mBirdMessage3);
		// mDataService.saveBirdMessage(mBirdMessage4);
		// mDataService.saveBirdMessage(mBirdMessage5);

		// mDataService.saveBirdMessage(mBirdMessage11);
		// mDataService.saveBirdMessage(mBirdMessage21);
		// mDataService.saveBirdMessage(mBirdMessage31);
		// mDataService.saveBirdMessage(mBirdMessage41);
		// mDataService.saveBirdMessage(mBirdMessage51);

	}

	private Handler refreshHander = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				finish();
				Intent intent = new Intent(mContext, UserLoginAct.class);
				mContext.startActivity(intent);
				break;
			case 0:
				finish();
//				User muser = (User) ValueStack.getInstance().getValue(
//				"USER");
				ValueStack.getInstance().setSessionValue("USER", mUser);
				Intent intent2 = new Intent(mContext, CollectionGridAct.class);
				mContext.startActivity(intent2);
				break;
			}
		}
	};
}